﻿using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Text;

namespace IconsAtlas
{
	class Program
	{
		static void Main(string[] args)
		{
			var bmp = new Bitmap(672, 656);
			var g = Graphics.FromImage(bmp);
			var x = 0;
			var y = 0;
			var css = new StringBuilder();

			css.Append(".icon{background-image:url(iconAtlas.png);background-repeat:no-repeat;width:16px;height:16px;}\r\n\r\n");

			// TODO: Change source folder
			foreach(var file in Directory.GetFiles(@"FOLDER CONTAINING THE ICONS", "*.png")) CHANGE_HERE;
			{
				var fileName = Path.GetFileNameWithoutExtension(file);

				Console.WriteLine("Generating {0}", fileName);

				var icon = Image.FromFile(file);
				var dx = Convert.ToInt32(Math.Floor(8d - (icon.Width / 2d)));
				var dy = Convert.ToInt32(Math.Floor(8d - (icon.Height / 2d)));

				g.DrawImage(icon, x + dx, y + dy, icon.Width, icon.Height);
				css.AppendFormat(".icon_{0}{{background-position:-{1}px -{2}px;}}\r\n", fileName, x, y);

				x += 16;

				if(x >= 672)
				{
					x = 0;
					y += 16;
				}
			}

			// TODO: Change destination folder
			File.WriteAllText(@"DESTINATION FOLDER\iconAtlas.css", css.ToString());  CHANGE_HERE;
			bmp.Save(@"DESTINATION FOLDER\iconAtlas.png", ImageFormat.Png);  CHANGE_HERE;
		}
	}
}
